from data_define import Record

import json


class FileRead:
    def read_data(self) -> list[Record]:
        pass


class JsonFileeader(FileRead):
    def __init__(self, path):
        self.path = path

    def read_data(self):
        rest_list: list[Record] = []
        f = open(self.path, "r", encoding="UTF-8")
        try:
            for line in f.readlines():
                line = line.strip()
                data_json = json.loads(line)
                record = Record(data_json["date"], data_json["order_id"], int(data_json["money"]), data_json["province"])
                rest_list.append(record)
        except Exception as e:
            print(e)
        finally:
            f.close()
        return rest_list


class TextFileReader(FileRead):
    def __init__(self, path):
        self.path = path

    def read_data(self):
        rest_list: list[Record] = []
        f = open(self.path, "r", encoding="UTF-8")
        try:
            for line in f.readlines():
                line = line.strip()
                data_list = line.split(",")
                record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
                rest_list.append(record)
        except Exception as e:
            print(e)
        finally:
            f.close()
        return rest_list


if __name__ == '__main__':
    file_path = "file.json"
    # reader = TextFileReader(file_path)
    reader = JsonFileeader(file_path)
    rest_list = reader.read_data()
    print(rest_list)
